<?php

require_once($INCLUDE_PATH.'app/controller/InitController.php');
require_once ($INCLUDE_PATH . "/inc/checkfun.php");
require_once ($INCLUDE_PATH . "/include/WebService2Db.php");
require_once($INCLUDE_PATH.'app/include/func.global.php');
require_once($INCLUDE_PATH.'app/model/uploadmodel.php');
function __autoload($class_name) {
    require_once APP_ROOT.'app/model/'.strtolower($class_name) . '.php';
}
class EmailController extends InitController {

    public function __construct() {
        parent::__construct();

    }

    //Number
    public function actionNumberinfo() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $email = new EmailModel();
        $email->db_name="_email_number";
        if($_REQUEST['do']=="add" || $_REQUEST['do']=="add_save") {
            $do = "add_save";
            $do_msg = "添加新";
        }
        if($_REQUEST['do']=="edit" || $_REQUEST['do']=="edit_save") {
            $do = "edit_save";
            $do_msg = "修改";
        }
        $e_id = (int)$_REQUEST['id'];
        if($e_id>0) {
            Y::$tpl->set("e_id",$e_id);
            $emailInfo = $email->getEmailInfo($e_id);
            Y::$tpl->set("emailInfo",$emailInfo);
        }
        Y::$tpl->set("do",$do);
        Y::$tpl->set("do_msg",$do_msg);

        if(count(explode("save",$_REQUEST['do']))>1) {
            if ($_REQUEST['do']=="add_save") {
                $s = array();
                $s['username'] = $_REQUEST['username'];
                $s['passworld'] = $_REQUEST['password'];
                $s['email_type'] = $_REQUEST['email_type'];
                $s['created_time']=date("Y-m-d H:i:s");
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                $s['created_id']=$userInfo['id'];
                $result = $email->saveEmailInfo($s);
            }
            if ($_REQUEST['do']=="edit_save") {
                $s = array();
                $s['username'] = $_REQUEST['username'];
                if(!empty($_REQUEST['password']))
                    $s['passworld'] = $_REQUEST['password'];
                $s['email_type'] = $_REQUEST['email_type'];
                //$s['created_time']=date("Y-m-d H:i:s");

                $result = $email->editEmailInfo($e_id,$s);
            }
            if ($result) {
                return self::sysMessage($do_msg."邮件成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage($do_msg."邮件失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        //End for explode(save)
        }
    //echo PRE.'hhj';
    }
    public function actionNumberlist() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $email = new EmailModel();
        $email->db_name="_email_number";
        if($_REQUEST['id']) {
            if ($_REQUEST['do'] == "del") {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = $email->delDomain($_REQUEST['id']);
                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 20;
        $sum = $email->getListCount();
        if (ceil ( $sum / $pageSize ) <= $page) {
            $page = ceil ( $sum / $pageSize );
        }
        $liststart=$pageSize * ($page - 1);
        $result = $email->getList($page,$liststart,$pageSize);
        $par = array();

        $showpage = Site_global::listPage($par, $sum, $page, $pageSize);
        Y::$tpl->set("showpage",$showpage);
        Y::$tpl->set("list",$result);
    }

    //Template
    public function actionTemplateinfo() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $email = new EmailModel();
        $email->db_name="_email_templet";
        if($_REQUEST['do']=="add" || $_REQUEST['do']=="add_save") {
            $do = "add_save";
            $do_msg = "添加新";
        }
        if($_REQUEST['do']=="edit" || $_REQUEST['do']=="edit_save") {
            $do = "edit_save";
            $do_msg = "修改";
        }
        $e_id = (int)$_REQUEST['id'];
        if($e_id>0) {
            Y::$tpl->set("e_id",$e_id);
            $emailInfo = $email->getEmailInfo($e_id);
            Y::$tpl->set("emailInfo",$emailInfo);
            $content = $emailInfo['content'];
        }
        Y::$tpl->set("do",$do);
        Y::$tpl->set("do_msg",$do_msg);
        $fckEditor=  GlobalModel::loadFck( "content",$content);
        Y::$tpl->set("content",$fckEditor);

        $itemcount = ItemModel::getListCount("");
        $item = ItemModel::getList(0,0,$itemcount);
        Y::$tpl->set("item",$item);
        if(count(explode("save",$_REQUEST['do']))>1) {
            if ($_REQUEST['do']=="add_save") {
                $s = array();
                $s['pid'] = $_REQUEST['pid'];
                $s['title'] = $_REQUEST['title'];
                $s['content'] = $_REQUEST['content'];
                $s['sender_name'] = $_REQUEST['sender_name'];
                $s['reply_email'] = $_REQUEST['reply_email'];
                $s['created_time']=date("Y-m-d H:i:s");
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                $s['created_id']=$userInfo['id'];
                $result = $email->saveEmailInfo($s);
            }
            if ($_REQUEST['do']=="edit_save") {
                $s = array();
                $s['pid'] = $_REQUEST['pid'];
                $s['title'] = $_REQUEST['title'];
                $s['content'] = $_REQUEST['content'];
                $s['sender_name'] = $_REQUEST['sender_name'];
                $s['reply_email'] = $_REQUEST['reply_email'];
                $s['edit_time']=date("Y-m-d H:i:s");
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                $s['edit_id']=$userInfo['id'];

                $result = $email->editEmailInfo($e_id,$s);
            }
            if ($result) {
                return self::sysMessage($do_msg."模板成功！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage($do_msg."模板失败！", array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        //End for explode(save)
        }
    //echo PRE.'hhj';
    }
    public function actionTemplatelist() {
        $degree = CheckLogin::checkUserDegree();
        if (!$degree) {
            die("<script LANGUAGE='javascript'>alert('您无权访问！');history.go(-1);</script>");
        }
        $email = new EmailModel();
        $email->db_name="_email_templet";
        if($_REQUEST['id']) {
            if ($_REQUEST['do'] == "del") {
                $userInfo = CheckFun::checkUserType($_COOKIE['adminname']);
                //print_r($userType);
                if($userInfo["user_type"] == 2) {
                    $del = $email->delUser($_REQUEST['id']);
                }else {
                    die("<script LANGUAGE='javascript'>alert('对不起，您无权删除！');history.go(-1);</script>");
                }
            }
        }
        if($_REQUEST['p']) {
            $page = $_REQUEST['p'];
        }else {
            $page = 1;
        }
        $pageSize = 20;
        $sum = $email->getListCount();
        if (ceil ( $sum / $pageSize ) <= $page) {
            $page = ceil ( $sum / $pageSize );
        }
        $liststart=$pageSize * ($page - 1);
        if ($_REQUEST['pid']) {
            $where = " where pid = ".$_REQUEST['pid'];
            Y::$tpl->set("ptitle",$_REQUEST['ptitle']);
        }else {

            $where = "";
        }
        $result = $email->getList($page,$liststart,$pageSize,$where);
        $par = array();

        $showpage = Site_global::listPage($par, $sum, $page, $pageSize,$where);
        Y::$tpl->set("list",$result);
        Y::$tpl->set("showpage",$showpage);
        Y::$tpl->set("list",$result);

    }
	    public function actionTypeCount() {
        $pid =$_REQUEST['pid'];
        $ptitle = $_REQUEST['ptitle'];
		$pageN = $_REQUEST['pn'];
		$s_time = $_REQUEST['start_time'];
		$e_time = $_REQUEST['end_time'];
		Y::$tpl->set("s_time",$s_time);
		Y::$tpl->set("e_time",$e_time);
		//if(empty($pageN))$pageN=20;
		//$s_time = $_REQUEST['start_time'];
		//$e_time = $_REQUEST['end_time'];
		if(empty($s_time))
			$s_time = 0;
		else
			$s_time = strtotime($s_time);
			
		if(empty($e_time))
			$e_time = 0;
		else
			$e_time = strtotime($e_time);
			
		if(!empty($s_time)&&!empty($e_time))
        	$pArray = EmailModel::emailCount($pid,$s_time,$e_time);
		else
			$pArray = EmailModel::emailCount($pid);
		
        Y::$tpl->set("l",$pArray);
        Y::$tpl->set("ptitle",$ptitle);
        $url=array('web','singleSmtp','smtpServer');
		//$url[0]='web';
		//$url[1]='singleSmtp';
		//$url[2]='smtpServer';
		
		if(!empty($s_time)&&!empty($e_time))
        	$dArray = EmailModel::typeCount($pid,$s_time,$e_time,$url);
		else
			$dArray = EmailModel::typeCount($pid,0,0,$url);
		//$dArray = EmailModel::urlCount($pid,$url);
		Y::$tpl->set("d",$dArray);
    }
	
    public function actionEmailCount() {
        $pid =$_REQUEST['pid'];
        $ptitle = $_REQUEST['ptitle'];
		$pageN = $_REQUEST['pn'];
		if(empty($pageN))$pageN=20;
        $pArray = EmailModel::emailCount($pid);
        Y::$tpl->set("l",$pArray);
        Y::$tpl->set("ptitle",$ptitle);
		$s_time = $_REQUEST['start_time'];
		$e_time = $_REQUEST['end_time'];
		if(empty($s_time))
			$s_time = 0;
		else
			$s_time = strtotime($s_time);
			
		if(empty($e_time))
			$e_time = 0;
		else
			$e_time = strtotime($e_time);

		$dArray = EmailModel::emailCount($pid,$s_time,$e_time,$pageN);
		Y::$tpl->set("d",$dArray);
    }
	
	public function actionUrlCount() {
        $pid =$_REQUEST['pid'];
        $ptitle = $_REQUEST['ptitle'];
		$pageN = $_REQUEST['pn'];
		$s_time = $_REQUEST['start_time'];
		$e_time = $_REQUEST['end_time'];
		Y::$tpl->set("s_time",$s_time);
		Y::$tpl->set("e_time",$e_time);
		//if(empty($pageN))$pageN=20;
		//$s_time = $_REQUEST['start_time'];
		//$e_time = $_REQUEST['end_time'];
		if(empty($s_time))
			$s_time = 0;
		else
			$s_time = strtotime($s_time);
			
		if(empty($e_time))
			$e_time = 0;
		else
			$e_time = strtotime($e_time);
			
		if(!empty($s_time)&&!empty($e_time))
        	$pArray = EmailModel::emailCount($pid,$s_time,$e_time);
		else
			$pArray = EmailModel::emailCount($pid);
		
        Y::$tpl->set("l",$pArray);
        Y::$tpl->set("ptitle",$ptitle);
        $url=self::$conf->confGroup(urlcount);
		if(!empty($s_time)&&!empty($e_time))
        	$dArray = EmailModel::urlCount($pid,$s_time,$e_time,$url);
		else
			$dArray = EmailModel::urlCount($pid,0,0,$url);
		//$dArray = EmailModel::urlCount($pid,$url);
		Y::$tpl->set("d",$dArray);
    }
    //Upload
    public function actionUpload() {
		$email = new EmailModel();
		$email->db_name="_send_email";
		if($_REQUEST['do']=="" || $_REQUEST['do']=="add_save") {
            $do = "add_save";
        }
		Y::$tpl->set("file",$_FILES."||");
		Y::$tpl->set("do",$do);
		$do_msg = "邮件上传";
		$pid = $_REQUEST['pid'];
		Y::$tpl->set("pid",$pid);
		$item = ItemModel::getOneItem($pid);
		Y::$tpl->set("ptitle",$item["title"]);
		
        if(count(explode("save",$_REQUEST['do']))>1) {
            if ($_REQUEST['do']=="add_save") {
				$file_str = UploadModel::upload($_FILES['userfile']);
				$file_name = $_FILES['userfile']['name'];
				$file_postfix = substr($file_name,strrpos($file_name,"."));
				//$file_name = str_replace($file_postfix,'',$_FILES['userfile']['name']);

				$target_dir = str_replace($file_postfix,'',$file_str).'/';
				//$z = new ZipModel();
				//$st = $z->Extract($file_str,$target_dir);
				//mkdir($target_dir,0777); 
				$line_array = $email->treatFile($file_str);
				while(list($key,$value)=each($line_array)) 
				{
					//$aa .=$value;
					$tempStr = explode(",",$value);
					//$pid = $tempStr[0];
					$email = $tempStr[0];
					$send_time = $tempStr[1];
					$sendtype = $tempStr[2];
					
					$s = array();
					$s['pid'] = $pid;
					$s['email'] = $email;
					$s['send_time'] = $send_time;
					$s['sendtype'] = $sendtype;
					$email = new EmailModel();
        			$email->db_name="_send_email";
					$result = $email->addUpload($s);
				}
				unlink($file_str);
				//$email->removeDir($target_dir);
				Y::$tpl->set("aa",$file_str);
            }
            if ($result) {
                return self::sysMessage($do_msg."成功！".$file_str, array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }else {
                return self::sysMessage($do_msg."失败！".$file_str, array("返回上一页" => "javascript:history.go(-1);", "关闭窗口" => "javascript:window.close();"));
            }
        //End for explode(save)
        }
		
    //echo PRE.'hhj';
    }
	
}

?>
